package com.bookstore.controller;

import com.bookstore.domain.Error;
import com.bookstore.domain.User;
import com.bookstore.service.UserService;
import com.bookstore.utils.JsonUtils;
import com.bookstore.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("user")
@Controller
public class UserController {
    @Autowired
    private UserService service;

    //跳转登录
    @RequestMapping("/login.do")
    public String login() {
        return "Login";
    }

    //登录操作
    @RequestMapping(value = "/doLogin.do")
    @ResponseBody//直接回写数据
    public String doLogin(User user, HttpSession session, HttpServletResponse rsp) {
        System.err.println(user);
        User loginUser = service.login(user);
        Error loginError = new Error();
        if (loginUser != null) {
            //判断用户状态，和用户类型
            if (loginUser.getStatus() == 0) {
                loginError.setErrorType(1);
                loginError.setErrorInfo("用户未激活，请先激活再试！");
                return JsonUtils.writeValueAsString(loginError);
            } else if (loginUser.getStatus() == 2) { //禁用
                loginError.setErrorType(1);
                loginError.setErrorInfo("账号被封禁，请联系管理员处理！");
                return JsonUtils.writeValueAsString(loginError);
            } else { //账户可以登录
                //存放登录信息 用于操作验证
                loginUser.setPassword(null);
                session.setAttribute("loginUser", loginUser);
                loginError.setErrorType(0);
                loginError.setErrorInfo("登陆成功！");
                return JsonUtils.writeValueAsString(loginError);
            }
        } else {
            loginError.setErrorType(1);
            loginError.setErrorInfo("用户名或密码错误！");
            return JsonUtils.writeValueAsString(loginError);
        }
    }

    @RequestMapping("/logout.do")
    public String loginOut(HttpSession session) {
        session.removeAttribute("loginUser");
        return "Login";
    }

    @RequestMapping("/activ.do")
    public String activation(String code) {
        service.activation(code);
        return "Login";
    }

    /**
     * 获取登录用户
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/getLoginInfo.do")
    @ResponseBody()
    public String getLoginInfo(HttpSession session) {
        Object loginUser = session.getAttribute("loginUser");
        return JsonUtils.writeValueAsString(loginUser);
    }


    @RequestMapping(value = "/getUser.do")
    @ResponseBody()
    public String getUserById(Integer uid) {
        User user = service.findById(uid);
        return JsonUtils.writeValueAsString(user);
    }

    /*
     * 根据条件获取所有用户
     * */
    @RequestMapping(value = "/getUsers.do")
    @ResponseBody
    public String getUsers(Integer start, Integer pageSize, HttpServletRequest req) {
        User user = (User) req.getSession().getAttribute("loginUser");
        Error error = new Error();
        //处理数据
        if (start == null) {
            start = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //从第一页开始，一页十行
        PageHelper.startPage(start, pageSize);
        String uType_str = req.getParameter("uType");
        String name = req.getParameter("name");
        Integer uType = -1;
        if (uType_str != null) {
            uType = Integer.parseInt(uType_str);
        }
        if (name == null || name.equals("")) {
            name = null;
        }
        if (user == null) {
            error.setErrorInfo("请先登录。。。");
            error.setErrorType(1);
        } else {
            if (user.getuType() == 2) {
                List<User> users = service.getUsers(uType, name);
                PageInfo<User> pages = new PageInfo<User>(users);
                //验证ok 查询返回集合
                return JsonUtils.writeValueAsString(pages);
            } else {
                error.setErrorInfo("权限不够。。。");
                error.setErrorType(4);
            }
        }
        //权限认证失败  返回错误信息
        return JsonUtils.writeValueAsString(error);
    }

    //用户禁用 模块
    @RequestMapping("/upStatus.do")
    @ResponseBody
    public String upStatus(Integer status, Integer uid) {
        Error er = new Error();
        int result = 0;
        result = service.upStatus(status, uid);
        if (result == 0) {
            er.setErrorType(5);
            er.setErrorInfo("操作失败。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("操作成功。。");
        }
        return JsonUtils.writeValueAsString(er);
    }

    //删除用户
    @RequestMapping("/delUser.do")
    @ResponseBody
    public String delUser(Integer uid) {
        Error er = new Error();
        int result = 0;
        result = service.delUser(uid);
        if (result == 0) {
            er.setErrorType(5);
            er.setErrorInfo("删除失败。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("删除成功。。");
        }
        return JsonUtils.writeValueAsString(er);
    }

    /*//添加用户 操作
    @RequestMapping("/doAddUser.do")
    @ResponseBody
    public String addUser(User user) {
        System.out.println(user);
        Error er = new Error();
        int result = 0;
        //设置激活码
        user.setCode(UuidUtil.getUuid());
        result = service.addUser(user);
        if (result == 0) {
            er.setErrorType(5);
            er.setErrorInfo("注册失败。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("注册成功。。");
        }
        return JsonUtils.writeValueAsString(er);
    }*/

    //修改密码
    @RequestMapping("/upUser.do")
    @ResponseBody
    public String upUser(User user) {
        System.err.println(user);
        Error er = new Error();
        int result = 0;
        //处理数据
        if (user.getPassword() == null || user.getPassword().equals("")) {
            user.setPassword(null);
        }
        System.err.println(user);
        result = service.upUser(user);
        if (result == 0) {
            er.setErrorType(5);
            er.setErrorInfo("修改失败。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("修改成功。。");
        }
        return JsonUtils.writeValueAsString(er);
    }

    //注册
    @RequestMapping("/regist.do")
    @ResponseBody
    public String regist(User user) {
        System.err.println(user);
        Error er = new Error();
        if(user == null){
            er.setErrorType(5);
            er.setErrorInfo("请填写好表单！");
            JsonUtils.writeValueAsString(er);
        }
        int result = 0;
        //设置激活码
        user.setCode(UuidUtil.getUuid());
        result = service.addUser(user);
        if (result == 0) {
            er.setErrorType(5);
            er.setErrorInfo("啊偶，网络开小差了。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("注册成功。。");
        }
        return JsonUtils.writeValueAsString(er);
    }
    //修改密码
}
